package com.example.backstage.service.Impl;

import cn.hutool.crypto.digest.DigestUtil;
import com.example.backstage.dao.ProductDao;
import com.example.backstage.service.ProductService;
import com.example.gok_mall.entity.Product;
import com.example.gok_mall.reponseData.ResponseCode;
import com.example.gok_mall.reponseData.ResultData;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.UUID;

    @Service
    public class ProductServiceImpl implements ProductService {
    @Autowired
    ProductDao productDao;

    @Override
    public ResultData UpdateProduct(Product product) {
        if(product!=null){
            if(StringUtils.isNotBlank(product.getSubImages())){
                String[] subImagesArray=product.getSubImages().split(",");
                if(subImagesArray.length>0){
                    product.setMainImage(subImagesArray[0]);
                }
            }
            if(product.getId()!=null){
                //代表是修改商品
                product.setUpdateTime(new Date());
                int rowCount =productDao.updateByPrimaryKeySelective(product);
                if(rowCount>0){
                    return ResultData.success("修改商品信息成功");
                }
                return ResultData.fail("修改商品信息失败");

            }else{
//                System.out.println("开始插入新增商品");
                //代表是新增商品
                product.setCreateTime(new Date());
                product.setUpdateTime(new Date());
                int rowCount=productDao.insertSelective(product);
//                System.out.println("执行完新增商品");
                if(rowCount>0){
//                    System.out.println("插入新增商品成功");
                    return ResultData.success("新增商品成功");
                }
                return ResultData.fail("新增商品失败");
            }
        }
        return ResultData.fail("新增或者更新产品参数不正确");
    }

    @Override
    public ResultData insertSelective(Product product) {
        int product1 = productDao.insertSelective(product);
        if (product != null) {
            return ResultData.success("添加商品成功");
        } else {
            return ResultData.fail("添加商品失败");
        }
    }

    @Override
    public ResultData deleteProduct(Integer id) {
        int product2 = productDao.deleteByPrimaryKey(id);
        if(id != null){
            return ResultData.success("删除商品成功");
        }else {
        return ResultData.fail("请输入商品id");
        }
    }

    @Override
    public ResultData<String> setSaleStatus(Integer productId, Integer status) {
            if(productId==null||status==null){
                return ResultData.fail(ResponseCode.ILLEGAL_ARGUMENT.getCode(),ResponseCode.ILLEGAL_ARGUMENT.getMage());
            }

            Product product=new Product();
            product.setId(productId);
            product.setStatus(status);

            int rowCount=productDao.updateByPrimaryKeySelective(product);
            if(rowCount>0){
                return ResultData.success("修改商品销售状态成功");
            }
            return ResultData.fail("修改商品销售状态失败");
        }
    }
